<template>
  <section>
    <div v-show="showHead">
      <XdoCard :bordered="false">
        <div ref="area_head">
          <XdoBreadCrumb show-icon>
            <XdoButton type="primary" class="dc-margin-right" @click="deleteRestore">删改恢复</XdoButton>
            <XdoButton type="primary" class="dc-margin-right" @click="deletionApplication">删单申请</XdoButton>
            <XdoButton type="primary" class="dc-margin-right" :loading="searchLoading" @click="handleSearchSubmit">查询</XdoButton>
            <XdoButton type="warning" class="dc-margin-right" @click="handleShowSearch">查询条件</XdoButton>
          </XdoBreadCrumb>
        </div>
        <div ref="area_search" >
          <div v-show="showSearch">
            <div class="separateLine"></div>
            <DcBillHeadXwtListSearch ref="listSearch"></DcBillHeadXwtListSearch>
          </div>
        </div>

      </XdoCard>
      <XdoCard :bordered="false">
        <div class="action" ref="area_actions">
          <template v-for="item in actions">
            <XdoButton v-if="item.needed === true" :type="item.type" :disabled="item.disabled" :loading="item.loading" style="font-size: 12px" :class="item.key"
                    @click="item.click" :key="item.label">
              <Icon :type="item.icon" size="22" class="xdo-icon"/>{{ item.label }}</XdoButton>&nbsp;
          </template>
          <ExportAsync :param="exportConfig" :click="handleAsyncExport" :columns="columnsInfo" />
        </div>

      </XdoCard>
      <XdoCard :bordered="false">
        <!--@on-row-dblclick="handleRowDblClick"-->
        <XdoTable class="dc-table" ref="table" :columns="gridConfig.gridColumns" :height="dynamicHeight"
                 :data="gridConfig.data" :loading="tableLoading" stripe :border="gridConfig.data.length > 0" @on-selection-change="handleSelectionChange"></XdoTable>
        <div ref="area_page">
          <XdoPage class="dc-page" :current="pageParam.page" :page-size='pageParam.limit' :total="pageParam.dataTotal" show-total show-sizer
                   @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="[15,20,30,40]"/>
        </div>
      </XdoCard>
    </div>
    <DcBillHeadXwtTabs v-if="!showHead" @onEditBack="editBack" :editConfig="editConfig"></DcBillHeadXwtTabs>

    <Modal v-model="decLogModal" title="历史申报状态" @on-ok="ok" @on-cancel="cancel" width='1000'>
      <DcDecLog ref='child' :head-id="headId" ></DcDecLog>
    </Modal>
    <ImportPage :importShow.sync="importShow" :importConfig="importConfig"></ImportPage>
  </section>
</template>
<script>
  import DcDecLog from '../../xwManageHistoricalState/dcDecLog/DcDecLogList'
  import DcBillHeadXwtListSearch from './DcBillHeadXwtListSearch'
  import DcBillHeadXwtTabs from './DcBillHeadXwtTabs'
  import { jx_bill_testUri,excelExport } from '@/api'
  import { getColumnsByConfig ,getExcelColumnsByConfig } from '@/common'
  import { columnsConfig,columns ,excelColumnsConfig } from './dcBillHeadXwtColumns'
  import { commonProcess } from '@/view/jx_bill_test-common'
  import { editStatus } from '@/view/jx_bill_test-common/constant'
  import ImportPage from 'xdo-import'
  export default {
    name: 'DcBillHeadXwtList'+'-PAGE',
    components: {
      DcBillHeadXwtListSearch,
      DcBillHeadXwtTabs,
      ImportPage,
      DcDecLog
    },
    mixins: [ columns, commonProcess ],
    data() {
      return {
        headId:'',
        decLogModal:false,
        showHead: true,
        showEdit: false,
        actions: [
          { type: 'text', disabled: false, click: this.emptyAll, icon: 'ios-trash-outline', label: '清空表体数据', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleAdd, icon: 'ios-add', label: '新增', key:'xdo-btn-add', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleDelete, icon: 'ios-trash-outline', label: '删除', key:'xdo-btn-delete', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleImport, icon: 'ios-cloud-upload-outline', label: '导入', key:'xdo-btn-upload', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleExport, icon: 'ios-cloud-download-outline', label: '同步导出', key:'xdo-btn-export', loading: false, needed: true },
        ],
        /* --------- 导入开始 ---------*/
        importShow:false, // 是否显示
        importConfig:{
          taskCode:'XXX', // 任务编码
          businessParam: {    // 自定义业务参数
            insertUser: this.$store.state.user.userNo,
            accessToken: this.$store.state.token,
            // 其余业务参数
          },
          systemCode: 'XXX',  // 系统标识
          tradeCode: this.$store.state.user.company,  // 企业编码
          httpApi: this.$http  // http_api接口对象
        },
        /* --------- 导入结束 ---------*/
        /* --------- 异步导出开始 ---------*/
        exportConfig:{
          taskCode: "xxx",  // 添加任务使用的taskCode
          systemCode: "xxx", // 添加任务使用的systemCode
          displayTaskCode: "", // 下载中心所使用的过滤条件，不填默认使用taskCode
          displaySystemCode: "", // 下载中心所使用的过滤条件，不填默认使用systemCode
          fileName: "",
          taskName: "",
          exportParamJson: "",
          enableSharding: true
          },
          columnsInfo:[],
        /* --------- 异步导出结束 ---------*/
      }
    },
    mounted() {
      this.gridConfig.gridColumns = getColumnsByConfig(this.totalColumns, columnsConfig)
      this.refreshDynamicHeight()
      this.getList()
      // 设置异步导出列
      this.columnsInfo = this.getAsyncExportColumns(this.gridConfig.gridColumns)
    },
    methods: {
      //删改恢复插入记录表
      insList(){
        this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.recoveryData,{
          billListNo:this.gridConfig.selectRows[0].billListNo,
          tradeCode:this.gridConfig.selectRows[0].tradeCode,
          emsNo:this.gridConfig.selectRows[0].emsNo,
          declareType:this.gridConfig.selectRows[0].declareType
        }).then(res => {
          this.$Message.info(res.data.message);
        }, () => {
        });
      },
      deleteHeadList(){
        this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.delHeadListByBillListNo,{billListNo:this.gridConfig.selectRows[0].billListNo}).then(res => {
          if (res.data.success) {
            alert(res.data.message)
            this.insertHeadList();
          }
        }, () => {
        });
      },
      insertHeadList(){
        this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.insertHeadList,{billListNo:this.gridConfig.selectRows[0].billListNo}).then(res => {
          if (res.data.success) {
            alert(res.data.message)
          }
        }, () => {
        });
      },
      //删改恢复逻辑校检
      deleteRestore(){
        if (this.gridConfig.selectRows.length==0){
          this.$Message.info('请勾选一条记录！');
          return;
        }else if (this.gridConfig.selectRows.length>1){
          this.$Message.info('仅允许单条数据进行恢复！');
          return;
        }
        this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.getHeadHisList,{billListNo:this.gridConfig.selectRows[0].billListNo}).then(res => {
          if (res.data.data=='0'){
            this.$Message.info('当前清单，没有历史备份数据，不能恢复！');
            return;
          } else {
            if ((this.gridConfig.selectRows[0].declareType=='2'||this.gridConfig.selectRows[0].declareType=='3'||this.gridConfig.selectRows[0].declareType=='5'||this.gridConfig.selectRows[0].declareType=='6')
              && (this.gridConfig.selectRows[0].status=='D1' || this.gridConfig.selectRows[0].status=='Z' || this.gridConfig.selectRows[0].status=='3' || this.gridConfig.selectRows[0].status=='A0')) {
              this.insList();
              if (this.gridConfig.selectRows[0].declareType=='3'||this.gridConfig.selectRows[0].declareType=='6'){
                this.deleteHeadList();
              }
            }else {
              this.$Message.info('当前数据不允许恢复，仅删改申请失败允许恢复！');
              return;
            }
          }
        }, () => {
        });

      },
      //删改申请:判断是否勾选一条数据
      deletionApplication(){
        if (this.gridConfig.selectRows.length!=1){
          this.$Message.info('每次只能选择一条数据进行删单删单申请！');
          return;
        }else {
          if (this.gridConfig.selectRows[0].status=='1' || this.gridConfig.selectRows[0].status=='4' || (this.gridConfig.selectRows[0].status=='3'&&(this.gridConfig.selectRows[0].declareType=='3' || this.gridConfig.selectRows[0].declareType=='6'))){
            this.editConfig.editStatus = editStatus.DELAPPLY
            this.editConfig.editData = this.gridConfig.selectRows[0]
            // this.editConfig.sid = this.gridConfig.selectRows[0].sid
            console.log(this.gridConfig.selectRows.length)
            this.showHead = false
            return;
          }
          this.$Message.info('仅‘海关终审通过’或‘预审批通过’的数据允许删单申请！');
          return;
        }

      },
      getList() {
        this.tableLoading = true
        this.$nextTick(() => {
          let params = Object.assign({}, this.getSearchParams())
          // 其他参数赋值
          //params.iEMark = "E"
          //params.emsNo = "E2325B0A0008"
          this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.list, params, {params: this.pageParam}).then(res => {
            if (res.data.success) {
              this.tableLoading = false
              this.gridConfig.data = res.data.data
              this.pageParam.dataTotal = res.data.total
            }
          }, () => {})
        })
      },
      handleEdit() {
          this.editConfig.editStatus = editStatus.EDIT
          this.editConfig.headId = this.gridConfig.selectData.sid
        this.editConfig.status=this.gridConfig.selectData.status
          this.editConfig.tradeMode=this.gridConfig.selectData.tradeMode
          this.editConfig.billListNo=this.gridConfig.selectData.billListNo
          this.editConfig.rotateApplyNo=this.gridConfig.selectData.rotateApplyNo
          this.editConfig.declareType=this.gridConfig.selectData.declareType
          this.editConfig.gMark=this.gridConfig.selectData.gMark
          this.editConfig.emsNo=this.gridConfig.selectData.emsNo
        console.log(this.editConfig.emsNo)
        console.log(this.editConfig.gMark)
          this.editConfig.editData = this.gridConfig.selectData
          this.showHead = false
      },
      handleView() {
          this.editConfig.editStatus = editStatus.SHOW
          this.editConfig.headId = this.gridConfig.selectData.sid
          this.editConfig.editData = this.gridConfig.selectData
          this.showHead = false
      },
      //历史状态弹框
      isShowStatic() {
        this.$refs.child.getList();
        this.decLogModal=true
      },
      //清空表体数据
      emptyAll(){

        if (this.gridConfig.selectRows.length==0 || this.gridConfig.selectRows.length>1){
          this.$Message.info('请勾选一条数据进行清空!');
          return;
        }else {
          //检验当前状态下清空是否能用
          if (this.gridConfig.selectRows[0].status=='B1' || this.gridConfig.selectRows[0].status=='C1' || this.gridConfig.selectRows[0].status=='0' ||
            this.gridConfig.selectRows[0].status=='1' || this.gridConfig.selectRows[0].status=='2' || this.gridConfig.selectRows[0].status=='DL' ||
            this.gridConfig.selectRows[0].status=='4' || this.gridConfig.selectRows[0].status=='5' || this.gridConfig.selectRows[0].status=='Y'){
            this.$Message.info('当前状态清空不可用!');
            return;
          }
          if (((this.gridConfig.selectRows[0].status=='D1' && this.gridConfig.selectRows[0].declareType!='1')||
            (this.gridConfig.selectRows[0].status=='3'&&this.gridConfig.selectRows[0].declareType!='1')||
            (this.gridConfig.selectRows[0].status=='Z'&&this.gridConfig.selectRows[0].declareType!='1'))){
            this.$Message.info('当前状态清空不可用!');
            return;
          }
          this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.deleteList,  {sid: this.gridConfig.selectRows[0].sid}).then(res => {
            if (res.data.success) {
              this.$Message.info(res.data.message);
            }
          }, () => {});
        }

      },
      handleAdd() {
        this.editConfig.editStatus = editStatus.ADD
        this.showHead = false
      },
      handleDelete() {
        //删除:允许用户对清单状态为暂存-AO、退单-3、入库失败-Z、申报失败-D1且申报类型DECLARE_TYPE=1
        for (let i=0;i<this.gridConfig.selectRows.length;i++){
          if (
            (this.gridConfig.selectRows[i].status!='A0'&&this.gridConfig.selectRows[i].status!='3'&&this.gridConfig.selectRows[i].status!='Z'&&this.gridConfig.selectRows[i].status!='D1'&&this.gridConfig.selectRows[i].declareType!='1')
            ||
            (this.gridConfig.selectRows[i].status!='A0'&&this.gridConfig.selectRows[i].status!='3'&&this.gridConfig.selectRows[i].status!='Z'&&this.gridConfig.selectRows[i].status!='D1'&&this.gridConfig.selectRows[i].declareType=='1')
          ){
            this.$Message.info('有不符合条件的数据存在，无法删除！');
            return;
          }
        }
        this.doDelete(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.delete)
      },
      editBack(val) {
        if(val){
          this.showHead = true
          this.gridConfig.selectRows = []
          this.getList()
        }
      },
      // 导入
      handleImport() {
        this.importShow = true
      },
      // 同步导出
      handleExport(){
        let columns = getExcelColumnsByConfig(this.totalColumns,excelColumnsConfig)
        let url = jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.export
        let exportName = 'xxx导出' // 根据需要，设置导出文件名称，待实现
        let params = {
          "header": columns,
          "name": exportName,
          "queryParam": this.getSearchParams()
        }
        excelExport(url,params)
      },
      // 异步导出
      handleAsyncExport(){
        this.exportConfig.taskName = "xxx导出"
        this.exportConfig.fileName = this.$store.state.user.userNo + this.exportConfig.taskName + "_" + this.getCurrentDateString() + ".xlsx"
        let queryParam = this.getSearchParams()
        delete queryParam.limit
        delete queryParam.page
        let params = {
          "queryParam": queryParam
        }
        this.exportConfig.exportParamJson = params.queryParam
      }
    }
  }
</script>
<style lang="less" scoped>
</style>
